package com.iflytek.springboot_medicine.service.impl;

import com.iflytek.springboot_medicine.entity.Category;
import com.iflytek.springboot_medicine.service.CategoryRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl {
    private final CategoryRepository categoryRespository;

    public CategoryServiceImpl(CategoryRepository categoryRespository) {
        this.categoryRespository = categoryRespository;
    }

    /**
     *分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<Category> findCategory(String name, int pageNum, int pageSize) {
        // 构建分页请求
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        // 执行分页查询
        return categoryRespository.findByName(name, pageable);
    }


    /**
     * 删除类别
     */
    public Category deleteCategory(int id) {
        return categoryRespository.deleteById(id);
    }


    /**
     * 批量删除
     */
    public void deleteAllInBatch(List<Integer> ids) {
        categoryRespository.deleteAllByIdInBatch(ids);
    }

    /**
     * 添加类别
     * @param category
     * @return
     */
    public Category addCategory(Category category) {
        return categoryRespository.save(category);
    }

    /**
     * 更新类别
     * @param category
     * @return
     */
    public Category updateCategory(Category category) {
        return categoryRespository.save(category);
    }

    public List<Category> findAllCategory() {
        return categoryRespository.findAll();
    }

}
